# Copyright 2020 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ===========================================================================
"""setup package."""
import os
import glob
import stat
import shutil
import setuptools

cache_folder = ('*egg-info', 'build')


def clean():
    for pattern in cache_folder:
        for name in glob.glob(pattern):
            if os.path.exists(name):
                shutil.rmtree(name)


def crlf_to_lf(file_path):
    with os.fdopen(os.open(file_path, os.O_RDONLY, 0o640), 'r', encoding='utf-8') as src:
        content = src.read()
    os.chmod(file_path, stat.S_IWRITE)
    with open(file_path, 'w', newline='\n', encoding='utf-8') as dst:
        dst.write(content.replace('\r\n', '\n'))


def handle_shell_scripts():
    cur_dir = os.path.abspath(os.path.dirname(__file__))
    for base, dirs, files in os.walk(os.path.join(cur_dir, 'ascend_deployer')):
        for file_name in files:
            file_path = os.path.join(base, file_name)
            if not file_path.endswith('.sh'):
                continue
            crlf_to_lf(file_path)


os.umask(stat.S_IRWXG | stat.S_IRWXO)
os.chmod('ascend_deployer/install.sh', stat.S_IEXEC | stat.S_IRUSR | stat.S_IRGRP | stat.S_IXGRP)
os.chmod('ascend_deployer/start_download.sh', stat.S_IEXEC | stat.S_IRUSR | stat.S_IRGRP | stat.S_IXGRP)
os.chmod('ascend_deployer/large_scale_install.sh', stat.S_IEXEC | stat.S_IRUSR | stat.S_IRGRP | stat.S_IXGRP)

clean()
handle_shell_scripts()

with open('README.rst', encoding='utf-8') as f:
    readme = f.read()

setuptools.setup(
    name='ascend-deployer',
    version='0.0.3',
    description='ascend offline installer',
    url='https://gitcode.com/ascend/ascend-deployer',
    long_description_content_type="text/x-rst",
    long_description=readme,
    classifiers=[
        'Programming Language :: Python :: 3',
        'License :: OSI Approved :: Apache Software License',
        'Operating System :: OS Independent'
    ],
    license='Apache',
    python_requires='>= 3.6',
    include_package_data=True,
    package_dir={'downloader': 'ascend_deployer/downloader', 'playbooks': 'ascend_deployer/playbooks'},
    packages=['ascend_deployer'],
    entry_points={  # Optional
        'console_scripts': [
            'ascend-download=ascend_deployer.ascend_download:main',
            'ascend-deployer=ascend_deployer.start_deploy:main',
            'large-scale-deployer=ascend_deployer.large_scale_deployer:main'
        ]
    },
)

clean()
